const app = getApp()

Page({
  data: {
    specialty: '',
    technicianInfo: null
  },

  onLoad() {
    // 获取工程师信息
    const technicianInfo = wx.getStorageSync('technicianInfo');
    if (technicianInfo) {
      this.setData({ 
        technicianInfo,
        specialty: technicianInfo.specialty || ''
      });
    } else {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }
  },

  onInput(e) {
    this.setData({ specialty: e.detail.value });
  },

  async saveSpecialty() {
    const { specialty, technicianInfo } = this.data;
    
    if (!specialty.trim()) {
      wx.showToast({
        title: '特长不能为空',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '保存中...',
      mask: true
    });

    try {
      const res = await app.request({
        url: `/api/technicians/${technicianInfo.id}`,
        method: 'PUT',
        data: {
          ...technicianInfo,
          specialty: specialty.trim()
        }
      });

      if (res.statusCode === 200) {
        // 更新本地存储的工程师信息
        const updatedTechnicianInfo = {
          ...technicianInfo,
          specialty: specialty.trim()
        };
        wx.setStorageSync('technicianInfo', updatedTechnicianInfo);

        wx.showToast({
          title: '保存成功',
          icon: 'success'
        });

        // 返回上一页
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      } else {
        throw new Error('保存失败');
      }
    } catch (error) {
      console.error('保存特长失败：', error);
      wx.showToast({
        title: '保存失败，请重试',
        icon: 'none'
      });
    } finally {
      wx.hideLoading();
    }
  }
}); 